package leetcode100;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class lc026 {
    public static void main(String[] args) {

        String s = "d";
        String maxString = new lc026().longestPalindrome(s);
        System.out.println("maxString = " + maxString);

    }

    /**
     * left起始索引; count字符串长度
     * left起始索引; count字符串长度
     */
    private Map<Integer, Integer> map = new HashMap<>();

    public void isHuiWen(char[] chars, int left, int right) {
        int count = left == right ? -1 : 0;
        System.out.println("left = " + left+" rihgt = "+right);
        while ((left >= 0)
                && (right < chars.length)
                && (chars[left] == chars[right])) {
            left--;
            right++;
            count += 2;
        }
        map.put(count, left+1);
//        System.out.println("map = " + map);
    }

    public String longestPalindrome(String s) {
        char[] chars = s.toCharArray();

        for (int i = 0; i < chars.length; i++) {
            isHuiWen(chars, i, i);
            isHuiWen(chars, i, i + 1);
        }
        Map.Entry<Integer, Integer> entry = map.entrySet()
                .stream()
                .max(Comparator.comparingInt(Map.Entry::getKey))
                .get();
        return s.substring(entry.getValue(), entry.getKey()+ entry.getValue());
    }
}
